package com.cowmooed.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cowmooed.entity.ModelEvaluationReport;
import com.cowmooed.mapper.ModelEvaluationReportMapper;
import com.cowmooed.service.ModelEvaluationReportService;
import com.cowmooed.utils.BaseContextUtil;
import com.cowmooed.vo.ModelEvaluationReportQueryVO;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 3D模型评估报告服务实现类
 * 实现3D模型评估报告的业务逻辑
 */
@Service
public class ModelEvaluationReportServiceImpl extends ServiceImpl<ModelEvaluationReportMapper, ModelEvaluationReport> implements ModelEvaluationReportService {

    /**
     * 查询当前登录用户的所有3D模型评估报告
     * @return 3D模型评估报告列表
     */
    @Override
    public List<ModelEvaluationReportQueryVO> queryAllReportsByCurrentUser() {
        Long userId = BaseContextUtil.getCurrentId();
        
        // 根据用户ID查询所有评估报告
        List<ModelEvaluationReport> reports = lambdaQuery()
                .eq(ModelEvaluationReport::getUserId, userId)
                .orderByDesc(ModelEvaluationReport::getUpdateTime)
                .list();
        
        // 转换为VO对象
        return reports.stream().map(this::convertToVO).collect(Collectors.toList());
    }
    
    /**
     * 将实体类转换为VO类
     * @param report 3D模型评估报告实体
     * @return 3D模型评估报告查询VO
     */
    private ModelEvaluationReportQueryVO convertToVO(ModelEvaluationReport report) {
        return ModelEvaluationReportQueryVO.builder()
                .id(report.getId())
                .model3dUrl(report.getModel3dUrl())
                .title(report.getTitle())
                .screenshot(report.getScreenshot())
                .readStatus(report.getReadStatus())
                .updateTime(report.getUpdateTime())
                .build();
    }
    
    /**
     * 将评估报告标记为已读
     * @param id 报告ID
     * @return 标记后的评估报告对象
     */
    @Override
    public ModelEvaluationReport markReportAsRead(Long id) {
        // 验证参数
        if (id == null || id <= 0) {
            throw new IllegalArgumentException("请选择需要查看详情的记录");
        }
        
        // 获取当前登录用户ID
        Long userId = BaseContextUtil.getCurrentId();
        
        // 先查询当前报告的状态
        ModelEvaluationReport report = lambdaQuery()
                .eq(ModelEvaluationReport::getId, id)
                .eq(ModelEvaluationReport::getUserId, userId)
                .one();
        
        // 如果报告存在且不是已读状态，则执行更新操作
        if (report != null && !ModelEvaluationReport.ReadStatus.READ.getValue().equals(report.getReadStatus())) {
            lambdaUpdate()
                    .eq(ModelEvaluationReport::getId, id)
                    .eq(ModelEvaluationReport::getUserId, userId) // 确保只能修改当前用户的报告
                    .set(ModelEvaluationReport::getReadStatus, ModelEvaluationReport.ReadStatus.READ.getValue())
                    .set(ModelEvaluationReport::getUpdateTime, LocalDateTime.now())
                    .update();
        }
        
        return report;
    }

}